<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>字体大小自动随宽度和内容改变 | Wentao&#39;s Blog</title>

  
  <meta name="author" content="wentao">
  

  
  <meta name="description" content="字体的大小设定可以在开始的时候指定.但是有时候需要根据窗口或者文字的长短来指定.那么这个时候就需要用到时间Paint了.
继承与Control的控件都有这个事件.也可以override OnPaint函数.
主要考虑两个因素,宽度和高度.比方如果字体的长度小于一个比例,则增加字体大小.如果大于一个比">
  

  
  
  <meta name="keywords" content=".Net">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="字体大小自动随宽度和内容改变"/>

  <meta property="og:site_name" content="Wentao&#39;s Blog"/>

  
  <meta property="og:image" content="/favicon.ico"/>
  

  <link href="/favicon.ico" rel="icon">
  <link rel="alternate" href="/atom.xml" title="Wentao&#39;s Blog" type="application/atom+xml">
  <link rel="stylesheet" href="/css/style.css" media="screen" type="text/css">
</head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/">Wentao&#39;s Blog</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
        <li><a href="/">主页</a></li>
      
        <li><a href="/archives">归档</a></li>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>字体大小自动随宽度和内容改变</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/2013/04/20/Auto-Change-Font-Size/" rel="bookmark">
        <time class="entry-date published" datetime="2013-04-20T07:30:13.000Z">
          2013-04-20
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <p>字体的大小设定可以在开始的时候指定.但是有时候需要根据窗口或者文字的长短来指定.那么这个时候就需要用到时间Paint了.
继承与Control的控件都有这个事件.也可以override OnPaint函数.
主要考虑两个因素,宽度和高度.比方如果字体的长度小于一个比例,则增加字体大小.如果大于一个比例则减小字体.</p>
<p>高度也是同样的道理.控制在一个比例范围以内就可以了.</p>
<p><figure class="highlight csharp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div></pre></td><td class="code"><pre><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> 标题重绘时候的处理</span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="sender"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="e"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></div><div class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">txtTitle_Paint</span>(<span class="params"><span class="keyword">object</span> sender, PaintEventArgs e</span>)</span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">var</span> lbl = sender <span class="keyword">as</span> Label;</div><div class="line">    <span class="keyword">if</span> (lbl == <span class="literal">null</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">return</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">var</span> size = MeasureStringWidth(lbl.Text, e.Graphics, lbl.Font);</div><div class="line">    <span class="keyword">float</span> diff = size.Width / lbl.Width;</div><div class="line">    <span class="keyword">var</span> font = lbl.Font;</div><div class="line">    <span class="keyword">while</span> ((diff &gt; <span class="number">0.6</span> || diff &lt; <span class="number">0.4</span>))</div><div class="line">    &#123;</div><div class="line"></div><div class="line">        font = <span class="keyword">new</span> Font(font.Name, diff &gt; <span class="number">0.5</span> ? font.Size - <span class="number">1</span> : font.Size + <span class="number">1</span>, font.Style);</div><div class="line">        size = MeasureStringWidth(lbl.Text, e.Graphics, font);</div><div class="line">        <span class="keyword">if</span> (size.Height &gt; lbl.Height)</div><div class="line">        &#123;</div><div class="line">            font = <span class="keyword">new</span> Font(font.Name, diff &gt; <span class="number">0.5</span> ? font.Size + <span class="number">1</span> : font.Size - <span class="number">1</span>, lbl.Font.Style);</div><div class="line">            <span class="keyword">break</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span> (font.Size &lt; <span class="number">5</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">break</span>;</div><div class="line">        &#125;</div><div class="line">        diff = size.Width / lbl.Width;</div><div class="line">    &#125;</div><div class="line">    lbl.Font = font;</div><div class="line">&#125;</div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> 获得文字的高宽</span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="text"&gt;</span>文字内容<span class="doctag">&lt;/param&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="graphic"&gt;</span>绘画<span class="doctag">&lt;/param&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="font"&gt;</span>字体<span class="doctag">&lt;/param&gt;</span></span></div><div class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span>大小<span class="doctag">&lt;/returns&gt;</span></span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SizeF <span class="title">MeasureStringWidth</span>(<span class="params"><span class="keyword">string</span> text, Graphics graphic, Font font</span>)</span></div><div class="line">&#123;</div><div class="line">    SizeF tmpSize = System.Windows.Forms.TextRenderer.MeasureText(text, font);</div><div class="line">    <span class="keyword">return</span> tmpSize;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    
      

    <span class="post-categories">
      <i class="icon-categories"></i>
        <a href="/categories/Dev/">Dev</a>
    </span>
    

    
    

    <span class="post-tags">
      <i class="icon-tags"></i>
        <a href="/tags/Net/">.Net</a>
    </span>
    

    </div>

    
  </div>
</article>

  
	<div id="comment">
	
	
	<!-- 多说评论框 start -->
	 <div class="ds-thread" data-thread-key="/2013/04/20/Auto-Change-Font-Size/" data-title="字体大小自动随宽度和内容改变" data-url="http://wentao.org/2013/04/20/Auto-Change-Font-Size/"></div>
	<!-- 多说评论框 end -->
	<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"wentao"};
	  (function() {
	    var ds = document.createElement('script');
	    ds.type = 'text/javascript';ds.async = true;
	    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
	    ds.charset = 'UTF-8';
	    (document.getElementsByTagName('head')[0] 
	     || document.getElementsByTagName('body')[0]).appendChild(ds);
	  })();
	  </script>
	<!-- 多说公共JS代码 end -->
	
	</div>




    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2016 wentao
    
  </p>
</footer>
    
  </div>
</div>
</body>
</html>